<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- <meta http-equiv="Content-Type" content="multipart/form-data;charset=utf-8" /> -->
<!-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -->
<title>首页 - 宠物寄养平台-后台管理系统</title>
<jsp:include page="headlink.jsp"></jsp:include>
</head>
<script type="text/javascript">
	function checkfuid() {
		var fuidflag = false;
		var reg1 = new RegExp("^[1-9]\d*|0$");
		var fuid = document.getElementById("addfuid").value;
		var flag1 = reg1.test(fuid);
		if (fuid == "") {
			document.getElementById("addfuidlabel").innerHTML = "<font color='red'>不能为空<font>";
			fuidflag = false;
		} else if (flag1) {
			$
					.ajax({
						'type' : 'get',
						'url' : '../fosterFamilyByfuid?fuid=' + fuid,//;+"&mrid="+Math.random(),
						async : false,//必须是同步提交的
						'success' : function(cm) {
							if (cm.msg === "不存在") {
								document.getElementById("addfuidlabel").innerHTML = "<font color=''>寄养家庭编号<font>";
								fuidflag = true;
							} else if (cm.msg === "寄养家庭已存在") {
								document.getElementById("addfuidlabel").innerHTML = "<font color='red'>寄养家庭存在<font>";
								fuidflag = false;
							} else if (cm.msg === "用户不存在") {
								document.getElementById("addfuidlabel").innerHTML = "<font color='red'>用户不存在<font>";
								fuidflag = false;
							}

						}
					});

		} else {
			document.getElementById("addfuidlabel").innerHTML = "<font color='red'>请输入非负整数<font>";
			fuidflag = false;
		}
		return fuidflag;

	}
	function checkfintroduction() {
		var fexperience = document.getElementById("addfintroduction").value;
		if (fexperience == "") {
			document.getElementById("addfintroduction1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("addfintroduction1").innerHTML = "<font color=''>自我介绍<font>";
			return true;
		}
	}
	function checkfrecepettype() {
		var fexperience = document.getElementById("addfrecepettype").value;
		if (fexperience == "") {
			document.getElementById("addfrecepettype1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("addfrecepettype1").innerHTML = "<font color=''>接收类型<font>";
			return true;
		}
	}
	function checkfservices() {
		var fexperience = document.getElementById("addfservices").value;
		if (fexperience == "") {
			document.getElementById("addfservices1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("addfservices1").innerHTML = "<font color=''>服务细则<font>";
			return true;
		}
	}
	function checkfexperience() {
		var reg1 = new RegExp("^[1-9]\d*|0$");
		var fexperience = document.getElementById("fexperience0").value;
		var flag1 = reg1.test(fexperience);
		if (fexperience == "") {
			document.getElementById("fexperience1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else if (flag1) {
			document.getElementById("fexperience1").innerHTML = "<font color=''>养宠经验<font>";
			return true;
		} else {
			document.getElementById("fexperience1").innerHTML = "<font color='red'>请输入年数<font>";
			return false;

		}
	}
	//编辑*******************************************************
	function checkfintroduction1() {
		var fexperience = document.getElementById("fintroduction").value;
		if (fexperience == "") {
			document.getElementById("fintroduction1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("fintroduction1").innerHTML = "<font color=''>自我介绍<font>";
			return true;
		}
	}
	function checkfrecepettype1() {
		var fexperience = document.getElementById("frecepettype").value;
		if (fexperience == "") {
			document.getElementById("frecepettype1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("frecepettype1").innerHTML = "<font color=''>接收类型<font>";
			return true;
		}
	}
	function checkfservices1() {
		var fexperience = document.getElementById("fservices").value;
		if (fexperience == "") {
			document.getElementById("fservices1").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else {
			document.getElementById("fservices1").innerHTML = "<font color=''>服务细则<font>";
			return true;
		}
	}
	function checkfexperience1() {
		var reg1 = new RegExp("^[1-9]\d*|0$");
		var fexperience = document.getElementById("fexperience00").value;
		var flag1 = reg1.test(fexperience);
		if (fexperience == "") {
			document.getElementById("fexperience10").innerHTML = "<font color='red'>不能为空<font>";
			return false;
		} else if (flag1) {
			document.getElementById("fexperience10").innerHTML = "<font color=''>养宠经验<font>";
			return true;
		} else {
			document.getElementById("fexperience10").innerHTML = "<font color='red'>请输入年数<font>";
			return false;

		}
	}
</script>

<body>
	<div class="lyear-layout-web">
		<div class="lyear-layout-container">
			<!--左侧导航-->
			<jsp:include page="leftsidebar.jsp"></jsp:include>
			<!--End 左侧导航-->

			<!--头部信息-->
			<jsp:include page="header.jsp"></jsp:include>
			<!--End 头部信息-->

			<!--页面主要内容-->
			<main class="lyear-layout-content">

			<div class="container-fluid">

				<div class="demoTable">
					搜索姓名：
					<div class="layui-inline">
						<input class="layui-input" name="id" id="demoReload"
							autocomplete="off">
					</div>
					<button class="layui-btn" data-type="reload">搜索</button>
				</div>
				<table class="layui-hide" id="test" lay-filter="test"></table>

				<!-- 添加按钮弹出层 -->
				<div class="layui-row" id="adduserinfo" style="display: none;">
					<div class="layui-col-md11">
						<form class="layui-form" action="" id='addmyform'
							enctype="multipart/form-data">
							<div class="layui-form-item" style="display: none;">
								<label class="layui-form-label" style="width: 118px">主键编号</label>
								<div class="layui-input-inline" style="margin: left:30px;">
									<input type="text" name="fid" class="layui-input"
										id="adduserName" value="" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="addfuidlabel">寄养家庭编号</label>
								<div class="layui-input-inline">
									<input type="text" name="fuid" class="layui-input" id="addfuid"
										value="" style="width: 212px" onblur="checkfuid()"
										placeholder="请输入寄养家庭编号">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="addfintroduction1">自我介绍</label>
								<div class="layui-input-inline">
									<textarea placeholder="请输入自我介绍" class="layui-textarea"
										name="fintroduction" id="addfintroduction"
										style="width: 212px" onblur="checkfintroduction()"></textarea>
									<!-- 									<input type="text" name="fintroduction" class="layui-input"
										id="adduserPwd" value=""> -->
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="addfrecepettype1">接收类型</label>
								<div class="layui-input-inline">
									<input type="text" name="frecepettype" class="layui-input"
										id="addfrecepettype" value="" style="width: 212px"
										placeholder="请输入接收类型" onblur="checkfrecepettype()">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px">环境图片</label>
								<div class="layui-input-block">
									<input type="file" name="fenvimageurl" style="width: 212px">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="addfservices1">服务细则</label>
								<div class="layui-input-inline">
									<input type="text" name="fservices" class="layui-input"
										id="addfservices" value="" style="width: 212px"
										placeholder="请输入服务细则" onblur="checkfservices()">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" id="fexperience1"
									style="width: 118px">养宠经验</label>
								<div class="layui-input-inline">
									<input type="text" name="fexperience" class="layui-input"
										id="fexperience0" value="" placeholder="请输入养宠年数"
										onblur="checkfexperience()" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px">状态</label>
								<div class="layui-input-inline">
									<select name="fstatus" lay-filter="state" id="adduserState">
										<option value="0" id="addoff" selected>未审核</option>
										<option value="1" id="addon">审核</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal"
										id="addsubbtn" lay-submit="" lay-filter="demo2"
										style="margin-left: 50px; padding-left: 22px; padding-right: 22px;">增加</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- 添加按钮弹出层 -->
				<!-- 编辑按钮弹出层 -->
				<div class="layui-row" id="edituserinfo" style="display: none;">
					<div class="layui-col-md11">
						<form class="layui-form" action="" id='myform'
							enctype="multipart/form-data">
							<div class="layui-form-item" style="display: none;">
								<label class="layui-form-label" style="width: 118px">主键编号</label>
								<div class="layui-input-inline">
									<input type="text" name="fid" class="layui-input" id="fid"
										value="" readonly="true" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px">寄养家庭编号</label>
								<div class="layui-input-inline">
									<input type="text" name="fuid" class="layui-input" id="fuid"
										value="" readonly="true" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="fintroduction1">自我介绍</label>
								<div class="layui-input-inline">
									<textarea placeholder="请输入自我介绍" class="layui-textarea"
										name="fintroduction" id="fintroduction" style="width: 212px"
										onblur="checkfintroduction1()"></textarea>
									<!-- 	<input type="text" name="fintroduction" class="layui-input"
										id="fintroduction" value="" style="width: 212px"> -->
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="frecepettype1">接收类型</label>
								<div class="layui-input-inline">
									<input type="text" name="frecepettype" class="layui-input"
										id="frecepettype" value="" style="width: 212px"
										placeholder="请输入接收类型" onblur="checkfrecepettype1()">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px">环境图片</label>
								<div class="layui-input-block">
									<input type="file" name="fenvimageurl" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px"
									id="fservices1">服务细则</label>
								<div class="layui-input-inline">
									<input type="text" name="fservices" class="layui-input"
										id="fservices" value="" style="width: 212px"
										placeholder="请输入服务细则" onblur="checkfservices1()">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" id="fexperience10"
									style="width: 118px">养宠经验</label>
								<div class="layui-input-inline">
									<input type="text" name="fexperience" class="layui-input"
										id="fexperience00" value="" placeholder="请输入养宠年数"
										onblur="checkfexperience1()" style="width: 212px">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width: 118px">状态</label>
								<div class="layui-input-inline">
									<select name="fstatus" lay-filter="state" id="fstatus">
										<option value="0" id="off">未审核</option>
										<option value="1" id="on" selected>审核</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal"
										id="subbtn" lay-submit="" lay-filter="demo1"
										style="margin-left: 50px; padding-left: 22px; padding-right: 22px;">编辑</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- 编辑按钮弹出层结束 -->

			</div>

			</main>
			<!--End 页面主要内容-->
		</div>
	</div>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/backmanage/js/jquery.min.js"></script>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/backmanage/js/bootstrap.min.js"></script>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/backmanage/js/perfect-scrollbar.min.js"></script>
	<script type="text/javascript"
		src="${pageContext.request.contextPath}/backmanage/js/main.min.js"></script>
	<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
	<button class="layui-btn layui-btn-sm" lay-event="adduser">添加用户</button>
  </div>
</script>


	<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-xs" lay-event="examine">审核</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
	<script
		src="${pageContext.request.contextPath}/backmanage/layui/layui.js"
		charset="utf-8"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

	<script>
		layui.use('table', function() {
			var table = layui.table;
			var form = layui.form;
			var url = table.render({
				elem : '#test',
				url : '../fosterFamilyBypage',
				toolbar : '#toolbarDemo',
				height : "full-110",
				cellMinWidth : 95,
				title : '用户数据表',
				cols : [ [ {
					type : 'checkbox',
					fixed : 'left'
				}, {
					field : 'fid',
					title : '用户编号',
					width : 120,
					sort : true,
					fixed : 'left'
				}, {
					field : 'uname',
					title : '寄养家庭',
					width : 120
				}, {
					field : 'fintroduction',
					title : '自我介绍',
					width : 180,
					sort : true
				}, {
					field : 'frecepettype',
					title : '接收类型',
					width : 130
				}, {
					field : 'fenvimageurl',
					title : '图片',
					width : 300,
					templet : function(res) {
						return "<div><img src=/ima/"+res.fenvimageurl+"></div>"
					}
				}, {
					field : 'fservices',
					title : '服务细则',
					width : 120,
					sort : true
				}, {
					field : 'fexperience',
					title : '养宠经验(年)',
					width : 120,
					sort : true,
				}, {
					field : 'fstatus',
					title : '状态',
					width : 80,
					templet : function(res) {
						return res.fstatus == 1 ? "审核" : "未审核"
					}
				}, {
					fixed : 'right',
					title : '操作',
					toolbar : '#barDemo',
					width : 199
				} ] ],
				page : true,
				id : 'testReload',
				limits : [ 2,10, 20, 50, 100 ],
				limit : 2
			});

			//头工具栏事件
			table.on('toolbar(test)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'getCheckData':
					var data = checkStatus.data;
					layer.alert(JSON.stringify(data));
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选');
					break;
				case 'adduser':
					layer.open({
						type : 1,
						title : "添加用户信息",
						skin : 'layui-layer-rim', //加上边框
						area : [ '500px', '580px' ], //宽高
						content : $("#adduserinfo"),
					});
					/* 渲染表单 */
					form.render();
					break;
				}
				;
			});

			//监听行工具事件
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				console.log(data.fuid);
				if (obj.event === 'del') {
					layer.confirm('真的删除吗？', function(index) {
						$.ajax({
							'type' : 'delete',
							'url' : '../FosterFamily/' + data.fuid+'/'+data.fid,
							'success' : function(cm) {
								layer.msg(cm.msg, {
									time : 2000
								}, function() {
									if (cm.msg == "删除成功") {
										layer.closeAll();
										table.reload('testReload');//重新加载列表
									} else if (cm.msg == "已有订单未完成，删除失败") {
										layer.msg("已有订单未完成，删除失败");
									} else {
										layer.msg("删除失败");
									}
								})
							}
						});
					})

				} else if (obj.event === 'edit') {
					$("#fid").attr("value", data.fid);
					$("#fuid").attr("value", data.fuid);
					$("#fintroduction").html(data.fintroduction);
					$("#fexperience00").attr("value", data.fexperience);
					//$("#fenvimageurl").attr("value", null);
					$("#frecepettype").attr("value", data.frecepettype);
					$("#fservices").attr("value", data.fservices);
					//console.log(data.fstatus);
					if (data.fstatus === 0) {
						$("#on").prop("selected", false);
						$("#off").prop("selected", true);
					} else {
						$("#on").prop("selected", true);
						$("#off").prop("selected", false);
					}

					//页面层
					layer.open({
						type : 1,
						title : "修改用户信息",
						skin : 'layui-layer-rim', //加上边框
						area : [ '500px', '580px' ], //宽高
						content : $("#edituserinfo"),
					});
					/* 渲染表单 */
					form.render();
				} else if (obj.event === 'examine') {

					//console.log(data.fid);
					layer.confirm('修改审核？', {
						btn : [ '确认', '取消' ]
					//按钮
					}, function() {
						$.ajax({
							'type' : 'get',
							'url' : '../updatefosterFamilyById?fid=' + data.fid
									+ "&fstatus=" + data.fstatus,
							'success' : function(cm) {
								layer.msg(cm.msg, {
									time : 2000
								}, function() {
									if (cm.msg == "已修改") {
										layer.closeAll();
										table.reload('testReload');//重新加载列表
									}
								})
							}
						});
					});
				}
			});

			var $ = layui.$, active = {
				reload : function() {
					var demoReload = $('#demoReload');
					//console.log(demoReload.val());
					//执行重载
					table.reload('testReload', {
						page : {
							curr : 1
						//重新从第 1 页开始
						},
						where : {
							content : demoReload.val()
						}
					});
				}
			};

			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});

			//编辑--监听提交
			layui.form.on('submit(demo1)', function(data) {
				var flag_checkfexperience = checkfexperience1();
				var flag_checkfintroduction = checkfintroduction1();
				var flag_checkfrecepettype = checkfrecepettype1();
				var flag_checkfservices = checkfservices1();
				if (flag_checkfexperience && flag_checkfintroduction
						&& flag_checkfrecepettype && flag_checkfservices) {
					var form = new FormData(document.getElementById("myform"));
					console.log(form);
					$.ajax({
						'type' : 'post',
						'url' : '../fosterFamily',
						'data' : form,
						'processData' : false,
						'contentType' : false,
						'success' : function(cm) {
							layer.msg(cm.msg, {
								time : 2000
							}, function() {
								if (cm.msg == "修改成功") {
									layer.closeAll();
									window.parent.location.reload();//重新加载列表
								}
							})
						}
					});
				}
			});

			//添加--监听提交
			layui.form.on('submit(demo2)', function(data) {
				var flag_checkfuid = checkfuid();
				var flag_checkfexperience = checkfexperience();
				var flag_checkfintroduction = checkfintroduction();
				var flag_checkfrecepettype = checkfrecepettype();
				var flag_checkfservices = checkfservices();
				//console.log(flag_checkfuid);
				if (flag_checkfuid && flag_checkfexperience
						&& flag_checkfintroduction && flag_checkfrecepettype
						&& flag_checkfservices) {

					var form = new FormData(document
							.getElementById("addmyform"));
					console.log(form);
					$.ajax({
						'type' : 'post',
						'url' : '../fosterFamilyadd',
						'data' : form,
						'processData' : false,
						'contentType' : false,
						'success' : function(cm) {
							layer.msg(cm.msg, {
								time : 2000
							}, function() {
								if (cm.msg == "增加成功") {
									layer.closeAll();
									table.reload('testReload');//重新加载列表
								}
							})
						}
					});
				}
			});
		});
	</script>

</body>